# $Id$
# Maintainer: Andreas Radke <andyrtr@archlinux.org>
# Contributor: Tom Newsom <Jeepster@gmx.co.uk>

pkgbase="sqlite"
pkgname=('sqlite' 'sqlite-tcl' 'sqlite-doc' 'sqlite-analyzer')
_srcver=3200100
_docver=${_srcver}
#_docver=3080001
pkgver=3.20.1
pkgrel=1
pkgdesc="A C library that implements an SQL database engine"
arch=('i686' 'x86_64')
license=('custom:Public Domain')
url="http://www.sqlite.org/"
makedepends=('tcl' 'readline')
source=(https://www.sqlite.org/2017/sqlite-src-${_srcver}.zip
        https://www.sqlite.org/2017/sqlite-doc-${_docver}.zip
        license.txt)
options=('!emptydirs' '!makeflags') # json extensions breaks parallel build
sha1sums=('808448837323b4ef9c0771af89d06dca5f0832dd'
          '9c2faf412173e51841d311e740d2061cad6544ad'
          'f34f6daa4ab3073d74e774aad21d66878cf26853')

prepare() {
  cd sqlite-src-$_srcver
#  autoreconf -vfi
}

build() {
  export CPPFLAGS="$CPPFLAGS -DSQLITE_ENABLE_COLUMN_METADATA=1 \
                             -DSQLITE_ENABLE_UNLOCK_NOTIFY \
                             -DSQLITE_ENABLE_DBSTAT_VTAB=1 \
                             -DSQLITE_ENABLE_FTS3_TOKENIZER=1 \
                             -DSQLITE_SECURE_DELETE \
                             -DSQLITE_MAX_VARIABLE_NUMBER=250000 \
                             -DSQLITE_MAX_EXPR_DEPTH=10000"

  # build sqlite
  cd sqlite-src-$_srcver
  ./configure --prefix=/usr \
	--disable-static \
	--disable-amalgamation \
	--enable-fts3 \
	--enable-fts4 \
	--enable-fts5 \
	--enable-rtree \
	--enable-json1 \
	TCLLIBDIR=/usr/lib/sqlite$pkgver
  make
  # build additional tools
  make showdb showjournal showstat4 showwal sqldiff sqlite3_analyzer
}

package_sqlite() {

 pkgdesc="A C library that implements an SQL database engine"
 depends=('readline')
 provides=("sqlite3=$pkgver")
 replaces=("sqlite3")

  cd sqlite-src-$_srcver
  make DESTDIR=${pkgdir} install

  install -m755 showdb showjournal showstat4 showwal sqldiff ${pkgdir}/usr/bin/

  # install manpage
  install -m755 -d ${pkgdir}/usr/share/man/man1
  install -m644 sqlite3.1 ${pkgdir}/usr/share/man/man1/

  # license - no linking required because pkgbase=pkgname
  install -D -m644 ${srcdir}/license.txt ${pkgdir}/usr/share/licenses/${pkgbase}/license.txt

  # split out tcl extension
  mkdir $srcdir/tcl
  mv $pkgdir/usr/lib/sqlite* $srcdir/tcl
}

package_sqlite-tcl() {

 pkgdesc="sqlite Tcl Extension Architecture (TEA)"
 depends=('sqlite')
 provides=("sqlite3-tcl=$pkgver")
 replaces=("sqlite3-tcl")

  install -m755 -d ${pkgdir}/usr/lib
  mv $srcdir/tcl/* ${pkgdir}/usr/lib

  # install manpage
  install -m755 -d ${pkgdir}/usr/share/man/mann
  install -m644 ${srcdir}/sqlite-src-$_srcver/autoconf/tea/doc/sqlite3.n ${pkgdir}/usr/share/man/mann/

  # link license
  install -m755 -d ${pkgdir}/usr/share/licenses
  ln -sf /usr/share/licenses/${pkgbase} "${pkgdir}/usr/share/licenses/${pkgname}"
}

package_sqlite-analyzer() {

 pkgdesc="An analysis program for sqlite3 database files"
 depends=('sqlite' 'tcl')

  cd sqlite-src-$_srcver
  install -m755 -d ${pkgdir}/usr/bin
  install -m755 sqlite3_analyzer ${pkgdir}/usr/bin/
}

package_sqlite-doc() {

 pkgdesc="most of the static HTML files that comprise this website, including all of the SQL Syntax and the C/C++ interface specs and other miscellaneous documentation"
 #arch=('any') - not yet supported
 provides=("sqlite3-doc=$pkgver")
 replaces=("sqlite3-doc")

  cd sqlite-doc-${_docver}
  mkdir -p ${pkgdir}/usr/share/doc/${pkgbase}
  cp -R *  ${pkgdir}/usr/share/doc/${pkgbase}/
}
